var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.get('/getStatistic',(req,res) =>{
    var params = req.query || req.params
    var sql = $sql.order.statistic;
    var seriesData=[]
    conn.query(sql,[params.flight_id],function(err,result){
        if(err){
            console.log(err);
        }
        if(result){
            console.log(result)
            let list = result
            for(let i = 0;i<list.length;i++){
                let item={value:0,name:""}
                item.value = parseInt(list[i].touristSum) + parseInt(list[i].businessSum) + parseInt(list[i].firstSum);
                item.name = list[i].flight_id.toString();
                seriesData.push(item)
            }
            console.log(seriesData)
            res.send(JSON.stringify({
                msg:"操作成功",
                status:200,
                data:seriesData
            }))
        }
    })
})

router.get('/getTicketList',(req,res) =>{
    var sql = $sql.ticket.count
    var params = req.query ||req.params
    let start = (params.page - 1) * 10;
    var allCount = 0
    console.log(params)
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.ticket.get
            conn.query(sql,[start],function(err,result){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var ticketList = result;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:ticketList
                    }))
                }
            })
        }
    })
})

router.get('/searchTicketList',(req,res) =>{
    var sql = $sql.ticket.searchCount
    var params = req.query ||req.params
    let start = (params.page - 1) * 10;
    let search = ""
    if(params.user_name){
        search +=`user_name=${new String(params.user_name)}`
        //search.user_id = parseInt(params.user_id)
    }
    if(params.flight_id){
        search +=` and flight_id=${parseInt(params.flight_id)}`
        //search.flight_id = parseInt(params.flight_id)
    }
    if(params.order_id){
        search +=` and order_id=${parseInt(params.order_id)}`
        //search.order_id = parseInt(params.order_id)
    }
    var allCount = 0
    console.log(params)
    conn.query(sql+search, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.ticket.searchList
            let limit = $sql.ticket.limit
            conn.query(sql+search+limit,[start],function(err,searchResult){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var ticketList = searchResult;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:ticketList
                    }))
                }
            })
        }
    })
})

router.post('/addTicket',(req,res)=>{
    var sql = $sql.ticket.add
    var params = req.body;
    params.create_time = new Date();
    params.modify_time = new Date();
    params.order_id = parseInt(params.order_id);
    params.flight_id = parseInt(params.flight_id);
    params.site_type = parseInt(params.site_type);
    console.log(params)
    conn.query(sql, [params], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/delTicket',(req,res)=>{
    var sql = $sql.ticket.delOrder
    var params = req.body;
    params.modify_time = new Date();
    params.order_id = parseInt(params.order_id);
    params.is_del = 1
    console.log(params)
    conn.query(sql, [params,params.order_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

module.exports = router;